package com.sctvcloud.dongqu.ui.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Pair;

import com.jiongbull.jlog.JLog;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.base.BaseFragment;
import com.sctvcloud.dongqu.ui.adapter.GreatestAdapter;
import com.sctvcloud.dongqu.ui.adapter.LiveHomeAdapter;
import com.sctvcloud.dongqu.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/7.
 * 首页直播模块相关
 */

public class LiveHomeDataAddHelper {

    private LiveHomeAdapter adapter;
    private BaseActivity activity;
    private BaseFragment fragment;

    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    public void setFragment(BaseFragment fragment) {
        this.fragment = fragment;
    }

    private Object syncObj;

    private int bannerPos = -1, advancePos = -1, livingTitlePos = -1, retrospectPos = -1, upPos;

    public LiveHomeDataAddHelper() {
        syncObj = new Object();
    }

    private void initAdapter(Context context, List<IListShowData> data) {
        adapter = new LiveHomeAdapter(context, data);
        advancePos = -1;
        bannerPos = -1;
        livingTitlePos = -1;
        retrospectPos = -1;
        upPos = -1;
    }

    private void initAdapter(Context context, List<IListShowData> data, ArrayList<Pair<String, String>> pair) {
        adapter = new LiveHomeAdapter(context, data);
        adapter.setPairs(pair);
        adapter.setActivity(activity);
        adapter.setFragment(fragment);
        advancePos = -1;
        bannerPos = -1;
        livingTitlePos = -1;
        retrospectPos = -1;
        upPos = -1;
    }

    private void initAdapter(@NonNull IListShowData data, Context context) {
        ArrayList<IListShowData> list = new ArrayList<>();
        list.add(data);
        this.initAdapter(context, list);
    }

    private void initAdapter(@NonNull IListShowData data, Context context, ArrayList<Pair<String, String>> pair) {
        ArrayList<IListShowData> list = new ArrayList<>();
        list.add(data);
        adapter = new LiveHomeAdapter(context, list);
        adapter.setPairs(pair);
        adapter.setActivity(activity);
        adapter.setFragment(fragment);
        advancePos = -1;
        bannerPos = -1;
        livingTitlePos = -1;
        retrospectPos = -1;
        upPos = -1;
    }

    public LiveHomeAdapter getAdapter(Context context) {
        synchronized (syncObj) {
            if (adapter == null) {
                initAdapter(context, null);
            }
        }
        return adapter;
    }

    public IListShowData getAdvanceData() {
        if (advancePos == 0) {
            return adapter.getItem(1);
        } else {
            return null;
        }
    }

    public IListShowData getAdvanceBannerData() {
        if (bannerPos == 0) {
            return adapter.getItem(0);
        } else {
            return null;
        }
    }

    public void setAdvanceData(Context context, IListShowData data, ArrayList<Pair<String, String>> pair) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            advancePos = addOrRefresh(context, LiveHomeAdapter.VIEWTYPE_ADVANCE - 1, advancePos, data, pair);
            if (advancePos >= 0) {
                upPos = advancePos;
                freshOtherPos(LiveHomeAdapter.VIEWTYPE_ADVANCE, advancePos, 1);
            }
        }
    }

    public void setAdvanceBannerData(Context context, IListShowData data, ArrayList<Pair<String, String>> pair) {
        if (data == null) {
            return;
        }
        synchronized (syncObj) {
            bannerPos = addOrRefresh(context, LiveHomeAdapter.VIEWTYPE_ADVANCE_BANNER - 1, bannerPos, data, pair);
            if (bannerPos >= 0) {
                upPos = bannerPos;
                freshOtherPos(LiveHomeAdapter.VIEWTYPE_ADVANCE_BANNER, bannerPos, 1);
            }
        }
    }

    public void removeAdvance() {
        synchronized (syncObj) {
            if (adapter == null || adapter.getItemCount() == 0 || advancePos < 0) {
                return;
            }
            adapter.removeByPostion(0);
        }
    }

    public void removeLiveDatas() {
        synchronized (syncObj) {
            if (adapter == null) {
                return;
            }
            if (advancePos == -1) {
                adapter.clean();
            } else {
                adapter.removeFrom(1);
            }
        }
    }

    public void addOrLiveDatas(Context context, List<IListShowData> datas, boolean isReset, ArrayList<Pair<String, String>> pair) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        synchronized (syncObj) {
            if (adapter == null) {
                initAdapter(context, datas, pair);
            } else if (isReset) {
                if (upPos < 0) {
                    adapter.setData(datas);
                } else {
                    adapter.resetDatasFrom(upPos, datas, true);
//                    if (upPos>=adapter.getItemCount()) {
//                        adapter.addDatas(datas);
//                    }else {
//                    }
                }

            } else {
                int pos = adapter.getItemCount();
                adapter.addDatas(datas);
                if (pos > 0) {
                    adapter.notifyItemChanged(pos - 1);
                }
//                adapter.addDatas(datas);
            }
        }
    }
//
//    public void addOrLiveDatas(Context context, List<IListShowData> datas, boolean isReset) {
//        if (datas == null || datas.size() == 0) {
//            return;
//        }
//        synchronized (syncObj) {
//            if (adapter == null) {
//                initAdapter(context, datas);
//            } else if (isReset) {
//                if (advancePos < 0) {
//                    adapter.setData(datas);
//                } else {
//                    adapter.resetDatasFrom(advancePos + 1, datas, true);
////                    if (upPos>=adapter.getItemCount()) {
////                        adapter.addDatas(datas);
////                    }else {
////                    }
//                }
//            } else {
//                adapter.addDatas(datas);
//            }
//        }
//    }

//    private int addOrRefresh(Context context, int targetPos, int temPos, @NonNull IListShowData data) {
//        if (targetPos == -1) {
//            return temPos;
//        }
//        if (adapter == null) {
//            initAdapter(data, context);
//            return 0;
//        } else {
//            if (temPos == -1) {
//                temPos = adapter.addData(data, getUpInsertPos(targetPos));
//            } else {
//                int pos = adapter.setDataInPos(data, temPos, false);
//                if (pos >= 0) {
//                    temPos = pos;
//                }
//            }
//        }
//        return temPos;
//    }

    private int getUpInsertPos(int targetPos) {
        if (targetPos == 0) {
            return targetPos;
        }
        if (upPos == -1) {
            return 0;
        }
        switch (targetPos + 1) {
//            case LiveHomeAdapter.VIEWTYPE_ADVANCE_BANNER:
//                if (bannerPos >= 0) {
//                    return bannerPos;
//                } else {
//                    targetPos = 0;
//                }
//                break;
            case LiveHomeAdapter.VIEWTYPE_ADVANCE:
                if (advancePos >= 0) {
                    return advancePos;
                } else {
                    targetPos = 0;
                }
                break;

            default:
                if (targetPos >= upPos) {
                    return upPos;
                }

        }
        return targetPos;
    }

    private int addOrRefresh(Context context, int targetPos, int temPos, @NonNull IListShowData data, ArrayList<Pair<String, String>> pair) {
        if (targetPos == -1) {
            return temPos;
        }
        if (adapter == null) {
            initAdapter(data, context, pair);
            return 0;
        } else {
            if (temPos == -1) {
                temPos = adapter.addData(data, getUpInsertPos(targetPos));
            } else {
                int pos = adapter.setDataInPos(data, temPos, false);
                if (pos >= 0) {
                    temPos = pos;
                }
            }
        }
        return temPos;
    }

    private synchronized void freshOtherPos(int mode, int pos, int diff) {
        boolean isAdd = diff > 0;
        switch (mode) {
            case LiveHomeAdapter.VIEWTYPE_ADVANCE_BANNER:
                if (advancePos >= 0) {
                    if (isAdd) {
                        if (advancePos <= pos) {
                            advancePos++;
                        }
                    } else {
                        advancePos--;
                        bannerPos = diff;
                        diff = advancePos;
                    }
                    pos = advancePos;
                }
                upPos = pos;
            case LiveHomeAdapter.VIEWTYPE_ADVANCE:
                if (!isAdd) {
                    advancePos = -1;
                    upPos--;
                } else {
                    upPos++;
                }
                break;

        }

    }

    public void removeAdItem() {
        synchronized (syncObj) {
            if (adapter == null || advancePos == -1) {
                return;
            } else {
                adapter.removeByPostion(0);
//                int pos = -1;
//                switch (mode) {
//                    case GreatestAdapter.VIEWTYPE_BANNER:
//                        pos = bannerPos;
//                        break;
//                    case GreatestAdapter.VIEWTYPE_EXPRESS:
//                        pos = expressPos;
//                        break;
//                    case GreatestAdapter.VIEWTYPE_LIVE:
//                        pos = livePos;
//                        break;
//                    case GreatestAdapter.VIEWTYPE_POPULAR:
//                        pos = posularPos;
//                        break;
//                }
//                if (adapter.removeByPostion(pos)) {
//                    freshOtherPos(mode, pos, -1);
//                }
            }
        }
    }


    public void onDestory() {
    }
}
